package com.hwadee.property.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.hwadee.property.pojo.Complain;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
@Mapper
public interface ComplainMapper extends BaseMapper<Complain> {
    // 计算符合条件的总记录数
    @Select("<script>" +
            "SELECT COUNT(*) FROM complain " +
            "<where>" +
            "   <if test='communityId != null'> AND community_id = #{communityId} </if> " +
            "   <if test='ownerId != null'> AND owner_id = #{ownerId} </if> " +
            "   <if test='status != null and status.trim() != \"\"'> " +
            "       AND processing_status = #{status} " +
            "   </if> " +
            "</where>" +
            "</script>")
    int countComplains(@Param("communityId") Long communityId,
                       @Param("ownerId") Long ownerId,
                       @Param("status") String status);

    // 分页查询符合条件的投诉记录
    @Select("<script>" +
            "SELECT * FROM complain " +
            "<where>" +
            "   <if test='communityId != null'> AND community_id = #{communityId} </if> " +
            "   <if test='ownerId != null'> AND owner_id = #{ownerId} </if> " +
            "   <if test='status != null and status.trim() != \"\"'> " +
            "       AND processing_status = #{status} " +
            "   </if> " +
            "</where> " +
            "LIMIT #{size} OFFSET #{offset}" +
            "</script>")
    List<Complain> getComplains(@Param("communityId") Long communityId,
                                @Param("ownerId") Long ownerId,
                                @Param("status") String status,
                                @Param("size") int size,
                                @Param("offset") int offset);
}
